<?php
session_start(); 
error_reporting(7);
require_once '../../includes/global.php';
require_once "../../includes/admins.php";
if($_GET[action]=='del')
{
  if($_GET[str]!='')
  {
	$db->query("update lm_info set ".$_GET[zd]."=REPLACE(".$_GET[zd].",'".$_GET[str]."#".$_GET[str1]."##', '')  WHERE id = '".$_GET[id]."'");
	unlink("/pic/".$_GET[str1]."");
			redirect($_SESSION[XS],'../.',$_SERVER['HTTP_REFERER'],"删除成功");
  }
  else
  {
  
			redirect($_SESSION[XS],'../.',$_SERVER['HTTP_REFERER'],"要删除的文件不存在");
  }

}

if($_POST[action]=="insert")
{

if($_POST[title]=='')
{
$_POST[title]="无名文件";
}
	if($_FILES['pic'][size]>0)
	{
		$temp=fetchaline("infosort","id='".$_POST[sortid]."'");
		$temp[$_POST[infozd]]=unserialize($temp[$_POST[infozd]]);
		//ce($temp[$_POST[infozd]]);
		$otype=explode(".",strtolower($_FILES['pic'][name]));

		if(strpos(strtolower($temp[$_POST[infozd]][value]),$otype[1]))
		{
		$time=time(); 
		$filename="../../pic/".$time.".".$otype[1];
		$copymes=copy($_FILES['pic']['tmp_name'] ,$filename);
		$_POST[$_POST[infozd]]=$_POST[title]."#".$time.".".$otype[1]."##";
		$_POST[userid]="admin";
		inserts('info',$_POST,1);
		$lastid=mysql_insert_id();
		?>
		<script language="javascript">
		parent.main.document.getElementById("id").value=<?php echo $lastid?>;
		parent.main.document.getElementById("action").value="modify";
		document.getElementById("id").value=<?php echo $lastid?>;
		document.getElementById("action").value="modify";
		</script>
		<?php
		redirect($_SESSION[XS],'../.',"getpic.php?action=modify&id=".$lastid."&sortid=".$_POST[sortid]."infozd=".$_POST[infozd]."",'操作成功！');

		}
		else
		{
				redirect($_SESSION[XS],'../.',$_SERVER['HTTP_REFERER'],'类型不符！');

		}
	}
	else
	{
				redirect($_SESSION[XS],'../.',$_SERVER['HTTP_REFERER'],'上传文件不能为空！');

	}
}



if($_POST[action]=="modify")
{
if($_POST[title]=='')
{
$_POST[title]="无名文件";
}
	if($_FILES['pic'][size]>0&&$_POST[id]!='')
	{
		$temp=fetchaline("infosort","id='".$_POST[sortid]."'");
		$temp[$_POST[infozd]]=unserialize($temp[$_POST[infozd]]);
		
		$otype=explode(".",strtolower($_FILES['pic'][name]));

		if(strpos(strtolower($temp[$_POST[infozd]][value]),$otype[1]))
		{
		$time=time(); 
		$filename="../../pic/".$time.".".$otype[1];
		$copymes=copy($_FILES['pic']['tmp_name'] ,$filename);
		$cp=fetchaline('info',"id='".$_POST[id]."'");
		$_POST[$_POST[infozd]]=$_POST[title]."#".$time.".".$otype[1]."##";
		$arr[$_POST[infozd]]=$cp[$_POST[infozd]].$_POST[$_POST[infozd]];
		updates('info',$arr,'id',$_POST[id]);
			redirect($_SESSION[XS],'../.',$_SERVER['HTTP_REFERER'],'操作成功！');
		}
		else
		{
			redirect($_SESSION[XS],'../.',$_SERVER['HTTP_REFERER'],'类型不符！');
		}
	}
	else
	{
			redirect($_SESSION[XS],'../.',$_SERVER['HTTP_REFERER'],'上传文件不能为空！');
	}
}

if($_GET[action]=='insert'&&$_GET[id]=='')
{
$nowstr="现在该条信息上没有绑定任何附件文件！";
}
else
{
 $temp=fetchaline('info',"id='".$_GET[id]."'");
 
  if($temp[$_GET[infozd]]!='')
  {
    $temp=explode("##",$temp[$_GET[infozd]]);
	 if($temp!='')
	 {
     foreach($temp as $value)
	 {
	$value=explode("#",$value);
	 if($value[1]!='')
	 {
		$nowstr.="<a href='/pic/".$value[1]."' target=_blank>".$value[0]."</a> [<a href='getpic.php?action=del&id=".$_GET[id]."&str=".$value[0]."&str1=".$value[1]."&zd=".$_GET[infozd]."'>删除</a>]|";
	  }
	 }
	 }
  }
  else
  {
$nowstr="现在该条信息上没有绑定任何附件文件！";
  }
}
?><head><META http-equiv=Content-Type content="text/html; charset=utf-8">
</head>

<table width="100%" border="0" cellpadding="3">
  <tr>
    <td bgcolor="#F8F8F8"><strong>现存文件</strong></td>
  </tr>
  <tr>
    <td  style="word-break: break-all; word-wrap:break-word;"><?php echo $nowstr;?></td>
  </tr>
</table>

<hr>
<form action="getpic.php" method="post" enctype="multipart/form-data" name="form1">
  <input type='hidden' name="sortid" value="<?php echo $_GET[sortid];?>">
  <input  type='hidden' name="id" value="<?php echo $_GET[id];?>">
  <input  type='hidden' name="action" value="<?php echo $_GET[action];?>">
  <input  type='hidden' name="infozd" value="<?php echo $_GET[infozd];?>">
  名称：
  <label>
  <input type="text" name="title">
  文件：
  </label>
  <input type="file" name="pic">
  <label>
  <input type="submit" name="Submit" value="提交">
  </label>
</form>
